package com.ruoyi.common.config;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
@ConfigurationProperties(prefix = "mysql")
@Data
public class ComlbsMySqlConfig {
    private static final Logger logger = LoggerFactory.getLogger(ComlbsMySqlConfig.class);

    private String sqlconnection;
    private String username;
    private String password;
    private Connection conn;

    public Connection getMysqlConneciton() throws SQLException {

        try {
            if (conn != null && conn.isValid(1)) return conn;
            conn = DriverManager.getConnection(sqlconnection, username, password);
            conn.setAutoCommit(false);
        } catch (Exception e) {
            logger.info("数据库连接失败！", e);
        }
        return conn;
    }
}
